<?php 

	include 'constants.php';
	include 'util.php';
	
	include 'Business/BusinessFactory.php';
	include 'DataAccess/DataAccessFactory.php';
	session_start();
	loadQueryXML();
	date_default_timezone_set("America/Sao_Paulo");
	
	$request = new HttpRequest();
	
	$request->method   = $_SERVER['REQUEST_METHOD'];
	$request->server   = $_SERVER['SERVER_NAME'];
	$request->url      = $_SERVER['REQUEST_URI'];
	
	// echo $request->server . "</br>";
	// echo $request->url;
	// die();
	/*======================================INICIO TESTE ===========================================*/
	//$json = array("userId" => 3 ,"identification" => "1111111111", "identificationType" => "identidade", "name" => "Patty", "birthDate" => "Nov 2, 3911 12:00:00 AM", "phone" => "88008802", "email" => "d2@gmail.com", "password" => "12345"); 
	
	//$json = array("notificationId" => null , "eventId" => null, "eventItemId"=>1, "title"=>"d2", "description"=> "p2", "notificationDate"=> "Nov 2, 3911 12:00:00 AM", "notificationUser" => array( "user" => null, "invitationUser" => null, "isUserManager" =>null, "isUserConfirmed" =>null , "userRegistrationCode" =>7));
	
	//$json = array("eventItemCommentId" => null, "eventItemId"=>1, "comment"=>"ueeeeeeeeeeeeeeepaaa", "date"=> null, "commentUser" => array("eventUser" => array( "user" => null, "invitationUser" => null, "isUserManager" =>null, "isUserConfirmed" =>null , "userRegistrationCode" =>7), "isUserSpeaker"=>null));
			  
	// $json = array("user" => array("identification" => null, "identificationType" => null, "name" => null, "birthDate" => null, "phone" => null, "email" => "d2s2@gmail.com", "password" => "12345"), 
				 // "sessionStart" => "2011-11-01 11:59:00");
						 
	// $json = array("requestId" => null, "service" => array("serviceId" => 1, "name" => ""), "roomGuestId"=> 1, 
				// "requestDate" => "Nov 25, 2011 12:00:00 AM", 
				// "employee" => null,
				// "responseDate" => null,
				// "requestItemList" => array(
				// array("requestItemId" => null, "requestId" => null, "description" => "leite", "quantity" => 2),
				// array("requestItemId" => null, "requestId" => null, "description" => "Pão com manteiga", "quantity" => 3),
				// array("requestItemId" => null, "requestId" => null, "description" => "Café", "quantity" => 1)
				// ));
// 			  
	// $_POST['request'] = json_encode($json);
	//$_POST['user'] = json_encode($json);
	//$_POST['isUserManager'] = false;
	//$_POST['isUserConfirmed'] = false;
	//$_POST['notification'] = json_encode($json);
	//$_POST['isUserSpeaker'] = true;
	//$_POST['userRegistrationCode'] = 1;
	
	//$_POST['session'] = json_encode($json);
	
	//$_POST['comment'] = json_encode($json);
	
	//$teste = '{"birthDate":"Sep 26, 3888 12:00:00 AM","email":"teste@teste.com","identification":"Identifucacao","identificationType":"RG","name":"Vinícius Lima Ribeiro","password":"12345","phone":"2187761988","userId":0}';
	//echo $_POST['user'];
	//$params = array();
	//$params['userRegistrationCode'] = 7;
	
	//$params['user'] = json_encode($json);
	//$params['isUserConfirmed'] = false;
	
	//$request->deleteArgs = $params; 
	//$request->method   = 'DELETE';
	//$request->method   = 'POST';
	
	// $params = array();
	// $params['user'] = json_encode($json);
// 	
	// $this->option(CURLOPT_POSTFIELDS, $params);
	/*======================================FIM TESTE ===========================================*/
	
	//Pegando os argumentos para a execucao da requisicao
	$request->getArgs  = $_GET;
	$request->postArgs = $_POST;
	$request->fileArgs = $_FILES;
	
	parse_str(file_get_contents('php://input'), $request->putArgs);
	parse_str(file_get_contents('php://input'), $request->deleteArgs);
	
	$arrayUrlParts = explode("/", $request->url);
	$strResource   = $arrayUrlParts[RESOURCE_URL_POSITION];
	$strFilter = isset($arrayUrlParts[FILTER_URL_POSITION]) ? $arrayUrlParts[FILTER_URL_POSITION] : "";
	$strFunction   = $strResource . $strFilter . $request->method;
	
	if($strResource != "") {
		$file          = getFilePath($strResource . RESOURCE_IDENTIFIER);
		require $file;
		
		if($request->method != 'POST') {
			$result = $strFunction($request);
			echo(html_entity_decode(json_encode($result)));
		} else {
			$strFunction($request);
		}		
	} else {
		echo "Bem-vindo ao servidor.";
	}	
 ?>